package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {
    @Autowired
    private IYddCateService iYddCateService;

    /**
     * 增加分类
     *
     * @param yddCate
     */
    @PostMapping("add")
    public ResultData save(@RequestBody YddCate yddCate) {
        log.info("ydd_cate:{}", yddCate);
        iYddCateService.save(yddCate);
        return ResultData.ok("增加成功", yddCate);
    }

    /**
     * 分类详情，数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData yddCate(Integer id) {
        log.info("根据id查询,id为:{}", id);
        YddCate yddCate = iYddCateService.getById(id);
        return ResultData.ok("数据回显", yddCate);
    }

    /**
     * 修改分类
     *
     * @param cate
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddCate cate) {
        log.info("修改分类信息,id为:{}", cate);
        iYddCateService.updateById(cate);
        return ResultData.ok("修改成功", cate);
    }

    /**
     * 分类分页查询
     *
     * @param page
     * @param limit
     * @param catename
     * @return
     */
    @GetMapping("list")
    public ResultData page(int page, int limit, String catename) {
        //1.构建分页构造器
        Page<YddCate> pageInfo = new Page<>(page, limit);
        //2.构造条件构造器进行模糊查询
        LambdaQueryWrapper<YddCate> law = new LambdaQueryWrapper<>();
        law.like(catename != null, YddCate::getCatename, catename);
        //3.增加排序条件
        law.orderByDesc(YddCate::getSortNum);
        //4.执行查询
        iYddCateService.page(pageInfo, law);
        //5.获取分页界面数据
        Map<String, Object> map = new HashMap<>();
        List<YddCate> records = pageInfo.getRecords();
        map.put("count", pageInfo.getTotal());
        map.put("totalPages", pageInfo.getPages());
        map.put("pageSize", pageInfo.getSize());
        map.put("currentPage", pageInfo.getCurrent());
        map.put("data", records);
        return ResultData.ok("分页查询成功", map);
    }
    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {
        log.info("删除分类,id为:{}", id);
        YddCate yddCate = iYddCateService.getById(id);
        String data = yddCate.getDefaultData();
        //判断DefaultData是否为默认值，如果为默认值则不能删除
        if (data != null && data.equals("1")) {
            return ResultData.error("默认数据，不能删除");
        } else {
            iYddCateService.removeById(id);
            return ResultData.ok("分类信息删除成功", "删除");
        }
    }

    /**
     * 分类列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        iYddCateService.list();
        return ResultData.ok("列表全部", iYddCateService.list());

    }


}
